home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility3 / netbios.zip / TALK.C < prev    next >
C/C++ Source or Header  |  1991-11-08  |  13KB  |  644 lines

  1.  
  2. /*
  3.  * Copyright (c) 1991 Darryl Collins.
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms are permitted
  7.  * provided that the above copyright notice and this paragraph are
  8.  * duplicated in all such forms and that any documentation,
  9.  * advertising materials, and other materials related to such
  10.  * distribution and use acknowledge that the software was developed
  11.  * by Darryl Collins, University of western Australia.  The name of the
  12.  * University may not be used to endorse or promote products derived
  13.  * from this software without specific prior written permission.
  14.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  15.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  16.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  17.  */
  18.  
  19.  
  20. /*
  21.  * This program is intended only as an example of the use of the NetBIOS DLL
  22.  *
  23.  * Please send questions, comments and bug-reports to
  24.  *
  25.  *               darryl@fibula.surgery.uwa.oz.au
  26.  *
  27.  */
  28.  
  29. #include "windows.h"
  30. #include "talk.h"
  31. #include "netbios.h"
  32.  
  33. #define LINELENGTH      200
  34. #define HISTORY         30
  35.  
  36. HANDLE  hInst;
  37. HANDLE  hWnd;
  38. HANDLE  hRecBox;
  39. HANDLE  hSendDlg; 
  40. HANDLE  hSysMenu;
  41.  
  42. char    cRemoteName[16];
  43. char    cLocalName[16];
  44.  
  45. LPSTR   lpSendBuffer;
  46. LPSTR   lpReceiveBuffer;
  47.  
  48. LPSTR   lpLocalName=cLocalName;
  49. LPSTR   lpRemoteName=cRemoteName;
  50.  
  51. FARPROC lpProcSendBox;
  52.  
  53. unsigned char           nLsn;
  54. unsigned char           nNum;
  55.  
  56.  
  57. SESSIONSTATUS   ssStatBuf;
  58.  
  59.  
  60. void ClearBuffer(LPSTR lpBuffer, int nLength);
  61.  
  62.  
  63.     
  64. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevious, LPSTR lpCmdLine, int nCmdShow) {
  65.  
  66.     MSG msgCurrent;
  67.     
  68.     lstrcpy(lpLocalName,lpCmdLine);
  69.  
  70.     if (!hPrevious) 
  71.     if (!InitApplication(hInstance))
  72.         return(FALSE);
  73.         
  74.     if (!InitInstance(hInstance, nCmdShow))
  75.     return(FALSE);
  76.     
  77.     while(GetMessage(&msgCurrent, NULL, NULL, NULL)) {
  78.         if(!hSendDlg || !IsDialogMessage(hSendDlg,&msgCurrent)) {
  79.             TranslateMessage(&msgCurrent);
  80.             DispatchMessage(&msgCurrent);
  81.         }
  82.     }
  83.     
  84.     return(msgCurrent.wParam);
  85. }
  86.  
  87.  
  88. BOOL InitApplication(HANDLE hInstance) {
  89.  
  90.     WNDCLASS    wcDefinition;
  91.     
  92.     wcDefinition.style = NULL;
  93.     wcDefinition.lpfnWndProc = MainWndProc;
  94.  
  95.     wcDefinition.cbClsExtra = 0;
  96.     wcDefinition.cbWndExtra = 0;
  97.     
  98.     wcDefinition.hInstance = hInstance;
  99.     wcDefinition.hIcon = LoadIcon(hInstance,"Icon");
  100.     wcDefinition.hCursor = LoadCursor(NULL, IDC_ARROW);
  101.     
  102.     wcDefinition.hbrBackground = GetStockObject(WHITE_BRUSH);
  103.     
  104.     wcDefinition.lpszMenuName = NULL;
  105.     wcDefinition.lpszClassName = "TalkClass";
  106.     
  107.     if(!(RegisterClass(&wcDefinition)))
  108.         return(FALSE);
  109.  
  110.     wcDefinition.style = NULL;
  111.     wcDefinition.lpfnWndProc = TTYProc;
  112.  
  113.     wcDefinition.cbClsExtra = 0;
  114.     wcDefinition.cbWndExtra = 0;
  115.     
  116.     wcDefinition.hInstance = hInstance;
  117.     wcDefinition.hIcon = LoadIcon(NULL,IDI_APPLICATION);
  118.     wcDefinition.hCursor = LoadCursor(NULL, IDC_ARROW);
  119.     
  120.     wcDefinition.hbrBackground = GetStockObject(WHITE_BRUSH);
  121.     
  122.     wcDefinition.lpszMenuName = NULL;
  123.     wcDefinition.lpszClassName = "TTYClass";
  124.     
  125.     return(RegisterClass(&wcDefinition));
  126. }
  127.  
  128.  
  129. BOOL InitInstance(HANDLE hInstance, int nCmdShow) {
  130.  
  131.     LPNCB   lpNcb;
  132.  
  133.     hInst = hInstance;
  134.  
  135.     hWnd = CreateWindow(
  136.     "TalkClass",
  137.     "Talk",
  138.     WS_OVERLAPPED|WS_ICONIC|WS_SYSMENU,
  139.         CW_USEDEFAULT,
  140.         CW_USEDEFAULT,
  141.         CW_USEDEFAULT,
  142.         CW_USEDEFAULT,
  143.     NULL,
  144.     NULL,
  145.     hInstance,
  146.     NULL
  147.     );
  148.     
  149.     if(!hWnd)  
  150.     return(FALSE);
  151.  
  152.     hSysMenu = GetSystemMenu(hWnd,0);
  153.     InsertMenu(hSysMenu,0,MF_SEPARATOR,(WORD)0,(LPSTR)0);
  154.     InsertMenu(hSysMenu,0,MF_STRING|MF_ENABLED,IDC_CALL,(LPSTR)"C&all");
  155.  
  156.     EnableWindow(hWnd,FALSE);
  157.  
  158.     ShowWindow(hWnd,nCmdShow|SW_MINIMIZE);
  159.     UpdateWindow(hWnd);
  160.  
  161.     lpNcb = AddName(hWnd, lpLocalName, NB_NOWAIT);
  162.     if(!lpNcb) {
  163.         MessageBox(hWnd,"Failed to add name", NULL, MB_OK);
  164.         DestroyWindow(hWnd);
  165.         return(FALSE);
  166.     }
  167.  
  168.     SendMessage(hWnd,WM_SETTEXT,0,(DWORD)(LPSTR)"Wait ...");
  169.  
  170.     return(TRUE);
  171. }
  172.  
  173.  
  174. LONG FAR PASCAL MainWndProc(HWND hWnd, unsigned uMessage, WORD wParam, LONG lParam) {
  175.  
  176.     FARPROC             lpRemoteProc;
  177.  
  178.     HDC                             hDC;
  179.     PAINTSTRUCT             ps;
  180.  
  181.     unsigned char   nRetCode;
  182.     LPNCB                   lpNcb;
  183.  
  184.     register int    i,j;
  185.  
  186.  
  187.     switch(uMessage) {
  188.  
  189.         case NB_COMMAND:
  190.             
  191.             lpNcb = (LPNCB)lParam;
  192.             nRetCode = lpNcb->NcbRetCode;
  193.  
  194.             switch(wParam) {
  195.  
  196.                 case NB_ADDNAME:
  197.  
  198.                     EnableWindow(hWnd,TRUE);
  199.  
  200.                     nNum = lpNcb->NcbNum;
  201.                     ReleaseNcb(lpNcb);
  202.  
  203.                     if(nRetCode) {
  204.                         MessageBox(hWnd,"Failed to Add Name",NULL,MB_OK|MB_ICONSTOP);
  205.                         DestroyWindow(hWnd);
  206.                         break;
  207.                     }
  208.  
  209.                     lpNcb = Listen(hWnd, (LPSTR)"*", lpLocalName, NB_NOWAIT);
  210.                     if(!lpNcb) {
  211.                         MessageBox(hWnd,"Listen Failed", NULL, MB_OK|MB_ICONSTOP);
  212.                         DestroyWindow(hWnd);
  213.                         return(FALSE);
  214.                     }
  215.  
  216.                     SendMessage(hWnd,WM_SETTEXT,0,(DWORD)(LPSTR)"Talk");
  217.  
  218.                     break;
  219.  
  220.                 case NB_LISTEN:
  221.  
  222.                     nLsn = lpNcb->NcbLsn;
  223.                     ReleaseNcb(lpNcb);
  224.  
  225.                     if(nRetCode) {
  226.                         MessageBox(hWnd,"Listen Failed",NULL,MB_OK|MB_ICONSTOP);
  227.                         DestroyWindow(hWnd);
  228.                         break;
  229.                     }
  230.  
  231.                     EnableMenuItem(hSysMenu,0,MF_BYPOSITION|MF_DISABLED|MF_GRAYED);
  232.                     
  233.                     lpNcb = SessionStatus(hWnd, (LPSTR)&ssStatBuf, 364, lpLocalName, NB_WAIT);
  234.                     ReleaseNcb(lpNcb);
  235.                     for(i=0; i<10; i++)
  236.                         if(ssStatBuf.ss[i].ssLsn == nLsn)
  237.                             break;
  238.  
  239.                     if(i == 10)
  240.                         break;
  241.  
  242.                     lstrcpy(lpRemoteName,&ssStatBuf.ss[i].ssRemoteName[0]);
  243.  
  244.                     lpProcSendBox = MakeProcInstance(SendBoxProc,hInst);
  245.  
  246.                     hSendDlg = CreateDialog(hInst, "SendBox", hWnd, lpProcSendBox);
  247.  
  248.                     break;
  249.  
  250.  
  251.                 case NB_CALL:
  252.  
  253.                     nLsn = lpNcb->NcbLsn;
  254.                     ReleaseNcb(lpNcb);
  255.  
  256.                     if(nRetCode) {
  257.                         MessageBox(hWnd,"Call Failed",NULL,MB_OK|MB_ICONSTOP);
  258.                         break;
  259.                     }
  260.  
  261.                     EnableMenuItem(hSysMenu,0,MF_BYPOSITION|MF_DISABLED|MF_GRAYED);
  262.                     
  263.                     lpProcSendBox = MakeProcInstance(SendBoxProc,hInst);
  264.  
  265.                     hSendDlg = CreateDialog(hInst, "SendBox", hWnd, lpProcSendBox);
  266.  
  267.                     break;
  268.  
  269.  
  270.                 default:
  271.                     ReleaseNcb(lpNcb);
  272.                     break;
  273.             }
  274.  
  275.             break;
  276.  
  277.  
  278.         case WM_SYSCOMMAND:
  279.  
  280.             switch(wParam&0xfff0) {
  281.  
  282.                 case IDC_CALL:
  283.  
  284.             lpRemoteProc = MakeProcInstance(RemoteDlgProc,hInst);
  285.             
  286.             if(DialogBox(hInst,"CallBox",hWnd,lpRemoteProc)) {
  287.                         lpNcb = Call(hWnd,lpRemoteName,lpLocalName,NB_NOWAIT);
  288.                         if(!lpNcb) {
  289.                             FreeProcInstance(lpRemoteProc);
  290.                             MessageBox(hWnd,"Call Failed",NULL,MB_OK|MB_ICONSTOP);
  291.                             break;
  292.                         }
  293.                     }
  294.  
  295.                     FreeProcInstance(lpRemoteProc);
  296.  
  297.                     break;
  298.  
  299.                 case SC_CLOSE:
  300.                     DestroyWindow(hWnd);
  301.                     break;
  302.  
  303.                 default:
  304.                     return(DefWindowProc(hWnd,uMessage,wParam,lParam));
  305.             }
  306.  
  307.             break;
  308.  
  309.  
  310.         case WM_CLOSE:    
  311.             DestroyWindow(hWnd);
  312.             break;
  313.  
  314.  
  315.     case WM_DESTROY:
  316.             if(nLsn) {
  317.                 lpNcb = HangUp(hWnd,nLsn,NB_WAIT);
  318.                 ReleaseNcb(lpNcb);
  319.             }
  320.  
  321.             if(nNum) {
  322.                 lpNcb = DeleteName(hWnd,lpLocalName,NB_WAIT);
  323.                 ReleaseNcb(lpNcb);
  324.             }
  325.  
  326.             if(lpReceiveBuffer)
  327.                 FreeFixedBuffer(lpReceiveBuffer);
  328.  
  329.         PostQuitMessage(0);
  330.         break;
  331.         
  332.     default:
  333.         return(DefWindowProc(hWnd, uMessage, wParam, lParam));
  334.     
  335.     }
  336.     
  337.     return(NULL);
  338. }
  339.  
  340.  
  341. LONG FAR PASCAL TTYProc(HWND hWnd, unsigned uMessage, WORD wParam, LONG lParam) {
  342.  
  343.     register int i,j,k;
  344.  
  345.     HDC                     hDC;
  346.     PAINTSTRUCT     ps;
  347.  
  348.     static RECT rcClient;
  349.     static int      X,Y;
  350.  
  351.     static char     cHistory[HISTORY][LINELENGTH];
  352.     static BOOL bHistory[HISTORY];
  353.     static int      nLine=0;
  354.  
  355.     static int      nScroll;
  356.     static BOOL     bScroll;
  357.  
  358.     long            lUnits;
  359.  
  360.  
  361.     switch(uMessage) {
  362.  
  363.         case WM_CREATE:
  364.             nLine = 0;
  365.  
  366.             lUnits = GetDialogBaseUnits();
  367.             X = LOWORD(lUnits);
  368.             Y = HIWORD(lUnits)/2;
  369.  
  370.             GetClientRect(hWnd,&rcClient);
  371.  
  372.             nScroll = (rcClient.bottom-rcClient.top)/Y/2;
  373.  
  374.             break;
  375.  
  376.         case WM_COMMAND:
  377.  
  378.             switch(wParam) {
  379.             
  380.                 case TTY_ADDLINE:
  381.                     lstrcpy((LPSTR)&cHistory[nLine][0],(LPSTR)lParam);
  382.                     bHistory[nLine] = ((LPSTR)lParam == lpReceiveBuffer);
  383.  
  384.                     nLine++;
  385.                     nLine %= HISTORY;
  386.                     if(nLine > nScroll)
  387.                         bScroll = TRUE;
  388.  
  389.                     InvalidateRect(hWnd,NULL,TRUE);
  390.                     break;
  391.             }
  392.  
  393.             break;
  394.  
  395.         case WM_PAINT:
  396.             hDC = BeginPaint(hWnd,&ps);
  397.  
  398.             if(bScroll) {
  399.                 j = nLine-nScroll;
  400.                 if(j < 0)
  401.                     j += HISTORY;
  402.             } else {
  403.                 j = 0;
  404.             }
  405.  
  406.             for(i=j,k=0; i<nLine; k++, i++, i%=HISTORY) {
  407.                 if(bHistory[i])
  408.                     SetTextColor(hDC,RGB(0,0,0));
  409.                 else
  410.                     SetTextColor(hDC,RGB(255,0,0));
  411.                 TextOut(hDC,X,Y*k*2,(LPSTR)&cHistory[i][0],lstrlen((LPSTR)&cHistory[i][0]));
  412.             }
  413.  
  414.             EndPaint(hWnd,&ps);
  415.             break;
  416.  
  417.             
  418.     default:
  419.         return(DefWindowProc(hWnd, uMessage, wParam, lParam));
  420.     
  421.     }
  422.     
  423.     return(NULL);
  424. }
  425.  
  426.  
  427. BOOL FAR PASCAL SendBoxProc(HWND hDlg, unsigned uMessage, WORD wParam, DWORD lParam) {
  428.  
  429.     LPNCB                   lpNcb;
  430.     unsigned char   nRetCode;       
  431.  
  432.     static HANDLE   hRecFrame;
  433.     static HANDLE   hSendBox;
  434.  
  435.     RECT                    rcClient;
  436.  
  437.     register int    i,j;
  438.  
  439.     LPSTR                   p;
  440.  
  441.  
  442.     switch(uMessage) {
  443.  
  444.         case WM_INITDIALOG:
  445.  
  446.             SendMessage(hDlg,WM_SETTEXT,(WORD)0,(DWORD)lpRemoteName);
  447.  
  448.             lpReceiveBuffer = AllocFixedBuffer(LINELENGTH);
  449.             if(!lpReceiveBuffer)
  450.                 return(TRUE);
  451.  
  452.             lpNcb = Receive(hDlg,nLsn,lpReceiveBuffer,LINELENGTH,NB_NOWAIT);
  453.             if(!lpNcb)
  454.                 return(TRUE);
  455.  
  456.             hRecFrame = GetDlgItem(hDlg,IDC_RECBOX);
  457.             hSendBox = GetDlgItem(hDlg,IDC_SENDBOX);
  458.  
  459.             GetClientRect(hRecFrame,&rcClient);
  460.  
  461.             hRecBox = CreateWindow(
  462.                 "TTYClass",
  463.                 "RecWindow",
  464.                 WS_CHILD|WS_VISIBLE|WS_BORDER,
  465.                 0,
  466.                 0,
  467.                 rcClient.right-rcClient.left,
  468.                 rcClient.bottom-rcClient.top,
  469.                 hRecFrame,
  470.                 NULL,
  471.                 hInst,
  472.                 NULL
  473.             );
  474.  
  475.             if(!hRecBox)
  476.                 PostMessage(hDlg,WM_COMMAND,IDC_HANGUP,(DWORD)0);
  477.  
  478.             SetFocus(hSendBox);
  479.  
  480.             return(FALSE);
  481.  
  482.  
  483.         case NB_COMMAND:
  484.  
  485.             lpNcb = (LPNCB)lParam;
  486.             nRetCode = lpNcb->NcbRetCode;
  487.             
  488.             switch(wParam) {
  489.  
  490.                 case NB_RECEIVE:
  491.  
  492.                     ReleaseNcb(lpNcb);
  493.  
  494.                     if(nRetCode) {
  495.                         if(nRetCode == 0x0a) {
  496.                             PostMessage(hDlg,WM_COMMAND,IDC_HANGUP,(DWORD)NULL);                                                    
  497.                             return(TRUE);
  498.                         }
  499.                         return(TRUE);
  500.                     }
  501.  
  502.                     SendMessage(hRecBox,WM_COMMAND,TTY_ADDLINE,(DWORD)lpReceiveBuffer);
  503.  
  504.                     ClearBuffer(lpReceiveBuffer,LINELENGTH);
  505.                     lpNcb = Receive(hDlg,nLsn,lpReceiveBuffer,LINELENGTH,NB_NOWAIT);
  506.  
  507.                     return(TRUE);
  508.  
  509.                 case NB_SEND:
  510.  
  511.                     FreeFixedBuffer(lpNcb->NcbBuffer);
  512.  
  513.                     ReleaseNcb(lpNcb);
  514.                     if(nRetCode) {
  515.                         if(nRetCode == 0x0a) {
  516.                             PostMessage(hDlg,WM_COMMAND,IDC_HANGUP,(DWORD)NULL);
  517.                             return(TRUE);
  518.                         }
  519.                     }
  520.  
  521.                     return(TRUE);
  522.  
  523.  
  524.                 default:
  525.                     ReleaseNcb(lpNcb);
  526.                     if(nRetCode) {
  527.                         if(nRetCode == 0x0a) {
  528.                             PostMessage(hDlg,WM_COMMAND,IDC_HANGUP,(DWORD)NULL);
  529.                             return(TRUE);
  530.                         }
  531.                     }
  532.                     return(TRUE);
  533.             }
  534.  
  535.         case WM_COMMAND:
  536.         
  537.             switch(wParam) {
  538.  
  539.                 case IDC_OK:
  540.  
  541.                     j = SendDlgItemMessage(hDlg,IDC_SENDBOX,EM_GETLINECOUNT,(WORD)0,(DWORD)0);
  542.  
  543.                     for(i=0; i<j; i++) {
  544.  
  545.                         lpSendBuffer = AllocFixedBuffer(LINELENGTH);
  546.                         if(!lpSendBuffer)
  547.                             return(TRUE);
  548.  
  549.                         lpSendBuffer[0] = LINELENGTH;
  550.  
  551.                         if(SendDlgItemMessage(hDlg,IDC_SENDBOX,EM_GETLINE,(WORD)i,(DWORD)lpSendBuffer)) {
  552.  
  553.                             SendMessage(hRecBox,WM_COMMAND,TTY_ADDLINE,(DWORD)lpSendBuffer);
  554.  
  555.                             lpNcb = Send(hDlg,nLsn,lpSendBuffer,lstrlen(lpSendBuffer),NB_NOWAIT);
  556.                             if(!lpNcb)
  557.                                 return(TRUE);
  558.                         }
  559.                     }
  560.  
  561.                     SendDlgItemMessage(hDlg,IDC_SENDBOX,WM_SETTEXT,0,(DWORD)(LPSTR)"");
  562.  
  563.                     SetFocus(hSendBox);
  564.  
  565.                     return(TRUE);
  566.  
  567.                 case IDC_CANCEL:
  568.  
  569.                     SendDlgItemMessage(hDlg,IDC_SENDBOX,WM_SETTEXT,(WORD)1,(DWORD)(LPSTR)"");
  570.                     return(TRUE);
  571.  
  572.  
  573.                 case IDC_HANGUP:
  574.                     
  575.                     if(nLsn) {
  576.                         lpNcb = HangUp(hDlg,nLsn,NB_WAIT);
  577.                         ReleaseNcb(lpNcb);
  578.                     }
  579.  
  580.                     EnableMenuItem(hSysMenu,0,MF_BYPOSITION|MF_ENABLED);
  581.  
  582.                     lpNcb = Listen(hWnd, (LPSTR)"*", lpLocalName, NB_NOWAIT);
  583.                     if(!lpNcb) {
  584.                         MessageBox(hWnd,"Listen Failed", NULL, MB_OK|MB_ICONSTOP);
  585.                         DestroyWindow(hWnd);
  586.                         return(FALSE);
  587.                     }
  588.  
  589.                     hSendDlg = 0;
  590.  
  591.                     DestroyWindow(hDlg);
  592.  
  593.                     return(TRUE);
  594.  
  595.             }
  596.             
  597.             break;
  598.     }
  599.  
  600.     return(FALSE);
  601. }
  602.  
  603.  
  604. BOOL FAR PASCAL RemoteDlgProc(HWND hDlg, unsigned uMessage, WORD wParam, DWORD lParam) {
  605.  
  606.     HANDLE  hEdit;
  607.  
  608.     switch(uMessage) {
  609.     
  610.     case WM_COMMAND:
  611.         
  612.         switch(wParam) {
  613.         case IDC_OK:
  614.                     GetDlgItemText(hDlg,IDC_REMOTE,lpRemoteName,16);
  615.                     EndDialog(hDlg,TRUE);
  616.                     return(TRUE);
  617.  
  618.         case IDC_CANCEL:
  619.             EndDialog(hDlg,FALSE);
  620.             return(TRUE);
  621.         }
  622.         
  623.         break;
  624.  
  625.     case WM_INITDIALOG:
  626.             
  627.             hEdit = GetDlgItem(hDlg,IDC_REMOTE);
  628.             SetFocus(hEdit);
  629.             
  630.         return(FALSE);
  631.     }
  632.     
  633.     return(FALSE);
  634. }
  635.  
  636.  
  637. void ClearBuffer(LPSTR lpBuffer, int nLength) {
  638.  
  639.     LPSTR   p = lpBuffer;
  640.  
  641.     while(p < &lpBuffer[nLength]) 
  642.         *p++ = '\0';
  643. }
  644.